#ifndef __MY_LINK_LIST_H__
#define __MY_LINK_LIST_H__
#include <iostream>
#include <cassert>

template <class T> 
class Node {
public:
    Node();
    Node(const T &elem, Node<T> *next = NULL);
    T data;

    Node<T> *NextNode();
    void insertAfter(Node<T> *n);

    Node<T> *deleteAfter();
    Node<T> *GetNode(const T &item, Node<T> *next = NULL);


private:
    Node<T> *next_;
};

template <class T>
Node<T>::Node() {}


template <class T>
Node<T>::Node(const T &item, Node<T> *next) {
    data = item;
    next_ = next;
}

template <class T>
Node<T> * Node<T>::NextNode() {
    return next_;
}

template <class T>
void Node<T>::insertAfter(Node<T> *n) {
    n->next_ = next_;
    next_ = n;
    return;
}

template <class T>
Node<T> * Node<T>::deleteAfter() {
    if (next_ == NULL) {
        return NULL;
    }

    Node<T> *n = next_;
    next_ = next_->next_;

    return n;
}

// Create a linkListNode
template <class T>
Node<T> *GetNode(const T &item, Node<T> *next = NULL) {
    Node<T> *newnode = new Node<T>(item, next);
    if (newnode == NULL) {
        assert(false && "Allocate node error");
        return NULL;
    }

    return newnode;
}

int array_size = 0;
double a[MAX_ELEM];

// Use index i to indicate node
double Next(int i) {
    if (i < size) {
        double ret = a[i + 1];
        i++;
        return ret;
    }
    else {
        return -inf;
    }
}

void insertAfter(int i, double d) {
    int j;
    for (j = array_size; j > i; j--) {
        a[j] = a[j - 1];
    }
    a[i] = d;
    array_size++;

    return;
}

double deleteAfter(int i) {
    int j;
    double ret = a[i];
    for (j = i; j < array_size - 1; j--) {
        a[j] = a[j + 1];
    }
    array_size--;
    return ret;
}


#endif

